package com.tang.common.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@ApiModel("返回数据")
@Data
@Accessors(chain = true)
@NoArgsConstructor
public class R<T> implements Serializable {
    @ApiModelProperty("状态码")
    private int code;
    @ApiModelProperty("提示信息")
    private String msg;
    @ApiModelProperty("数据对象")
    private T data;

    public R(int code){
        this.code=code;
    }
    public  R(int code,String msg){
        this.code=code;
        this.msg=msg;
    }
    public  R(int code,String msg,T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }

    public static <T> R<T> success(){
        return new R<>(ResultCode.SUCCESS,"操作成功");
    }
    public static <T> R<T> success(T data){
        return new R<>(ResultCode.SUCCESS,"操作成功",data);
    }

    public static <T> R<T> error(){
        return error(ResultCode.ERROR,"操作失败");
    }
    public static <T> R<T> error(String msg){
        return new R<>(ResultCode.ERROR,msg);
    }
    public static <T> R<T> error(int code,String msg){
        return new R<>(code,msg);
    }

    public boolean isSuccess(){
        return ResultCode.SUCCESS == this.code;
    }
}
